import 'package:flutter/cupertino.dart';
import 'package:gameplay_flutter/m_me/model/follow_model.dart';
import 'package:get/get.dart';

import '../../lib_basemodel/user/user.dart';
import '../../lib_network/http_helper.dart';
import '../me_api.dart';

class MeFollowersController extends GetxController {
  var followers = <FollowModel>[].obs;
  var hasMore = true.obs;

  getUsers(int type, int pageNum,{isShowLoading = true}) async {
    switch (type) {
      case 1:
        getFollowing(pageNum,isShowLoading);
        break;
      case 2:
        getFans(pageNum,isShowLoading);
        break;
    }
  }

  getFans(int pageNum,bool isShowLoading) async {
    var res = await HttpHelper.instance().post(
      MeApi.meFans,
      body: {"pageNum": pageNum, "pageSize": "40", "activeType": 0},
      showLoading: isShowLoading,
      showToast: true,
    );
    if (res.isSuccess && res.result is Map<String, dynamic>) {
      List<dynamic> listJson = res.result["list"];
      List<FollowModel> followList = listJson.map((json) {
        return FollowModel.fromJson(json);
      }).toList();
      if (pageNum == 1) {
        followers.clear();
      }
      hasMore.value = followList.length % 40 == 0;
      debugPrint("hasMore =  ${hasMore.value}");
      followers.addAll(followList);
    }
  }

  getFollowing(int pageNum,bool isShowLoading) async {
    var res = await HttpHelper.instance().post(
      MeApi.meFollowing,
      body: {"pageNum": pageNum, "pageSize": "40", "activeType": 0},
      showLoading: isShowLoading,
      showToast: true,
    );
    if (res.isSuccess && res.result is Map<String, dynamic>) {
      List<dynamic> listJson = res.result["list"];
      List<FollowModel> followList = listJson.map((json) {
        return FollowModel.fromJson(json);
      }).toList();
      if (pageNum == 1) {
        followers.clear();
      }
      hasMore.value = followList.length % 40 == 0;
      followers.addAll(followList);
    }
  }

  getVisitors(int pageNum,bool isShowLoading) async {
    var res = await HttpHelper.instance().post(
      MeApi.meVisitors,
      body: {
        "pageNum": pageNum,
        "pageSize": "40",
      },
      showLoading: isShowLoading,
      showToast: true,
    );
    if (res.isSuccess && res.result is Map<String, dynamic>) {
      List<dynamic> listJson = res.result["list"];
      List<FollowModel> followList = listJson.map((json) {
        return FollowModel.fromJson(json);
      }).toList();
      if (pageNum == 1) {
        followers.clear();
      }
      followers.addAll(followList);
      hasMore.value = followList.length % 40 == 0;
    }
  }

  getHistory(int pageNum,bool isShowLoading) async {
    var res = await HttpHelper.instance().post(
      MeApi.meHistory,
      body: {"pageNum": pageNum, "pageSize": "40"},
      showLoading: isShowLoading,
      showToast: true,
    );
    if (res.isSuccess && res.result is Map<String, dynamic>) {
      List<dynamic> listJson = res.result["list"];
      List<FollowModel> followList = listJson.map((json) {
        return FollowModel.fromJson(json);
      }).toList();
      if (pageNum == 1) {
        followers.clear();
      }
      followers.addAll(followList);
      hasMore.value = followList.length % 40 == 0;
    }
  }
}
